import {
  translate
} from '../../utils/translate'
import Notify from '@vant/weapp/notify/notify';
const app = getApp()

Page({
  data: {
    content: '',
    langOptions: [
      {
        text: '中文',
        value: 'zh'
      },
      {
        text: '英语',
        value: 'en'
      },
      {
        text: '粤语',
        value: 'yue'
      },
      {
        text: '文言文',
        value: 'wyw'
      },
      {
        text: '日语',
        value: 'jp'
      },
      {
        text: '韩语',
        value: 'kor'
      },
      {
        text: '法语',
        value: 'fra'
      },
      {
        text: '西班牙语',
        value: 'spa'
      },
      {
        text: '泰语',
        value: 'th'
      },
      {
        text: '阿拉伯语',
        value: 'ara'
      },
      {
        text: '俄语',
        value: 'ru'
      },
      {
        text: '葡萄牙语',
        value: 'pt'
      },
      {
        text: '德语',
        value: 'de'
      },
      {
        text: '意大利语',
        value: 'it'
      },
      {
        text: '希腊语',
        value: 'el'
      },
      {
        text: '荷兰语',
        value: 'nl'
      },
      {
        text: '波兰语',
        value: 'pl'
      },
      {
        text: '保加利亚语',
        value: 'bul'
      },
      {
        text: '爱沙尼亚语',
        value: 'est'
      },
      {
        text: '丹麦语',
        value: 'dan'
      },
      {
        text: '芬兰语',
        value: 'fin'
      },
      {
        text: '捷克语',
        value: 'cs'
      },
      {
        text: '罗马尼亚语',
        value: 'rom'
      },
      {
        text: '斯洛文尼亚语',
        value: 'slo'
      },
      {
        text: '瑞典语',
        value: 'swe'
      },
      {
        text: '匈牙利语',
        value: 'hu'
      },
      {
        text: '繁体中文',
        value: 'cht'
      },
      {
        text: '越南语',
        value: 'vie'
      }
    ],
    from: 'en',
    translateResult: ''
  },
  addLog() {
    app.globalData.logs.push({
      id: new Date().getTime(),
      origin: this.data.content,
      result: this.data.translateResult
    })
  },
  async translate() {
    if (!this.data.content) {
      return Notify('翻译文本不得为空!');
    }
    try {
      const {
        trans_result: [{
          dst: translateResult
        }]
      } = await translate({
        q: this.data.content,
        to: this.data.from
      })
      this.setData({
        translateResult
      })
      this.addLog()
    } catch (error) {
      console.error('翻译出错了', error);
    }
  },
  copy() {
    wx.setClipboardData({
      data: this.data.translateResult,
      success: () => {
        wx.showToast({
          title: '复制成功'
        });
      }
    });
  },
  dropdownChange(e) {
    this.setData({
      to: e.detail
    })
    this.translate()
  }
})
